package com.magic.smartcity.view.more.models

import com.magic.smartcity.beans.MetroCurrentStationLines
import com.magic.smartcity.beans.MetroLine
import com.magic.smartcity.beans.MetroLines
import com.magic.smartcity.beans.MetroStep
import com.magic.smartcity.utils.HttpClient
import com.magic.smartcity.view.more.models.services.MetroService
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers
import retrofit2.Retrofit

/**
 * @description:
 * @create: Create by Magic on 2022/2/11.
 */

class MetroModel {
    private val TAG: String = "MetroModel"

    private val service: MetroService by lazy {
        HttpClient().retrofit.create(MetroService::class.java)
    }


    /**
     * 获取地铁所有线路
     */
    fun getMetroAllLineList(success: (List<MetroLine>) -> Unit, fail: (String) -> Unit) {
        service.getMetroAllLineList().observeOn(Schedulers.io())
            .subscribeOn(AndroidSchedulers.mainThread())
            .doOnError { it.printStackTrace() }
            .subscribe {
                if (it.code == 200) {
                    success(it.data)
                } else {
                    fail(it.msg)
                }
            }
    }

    /**
     * 经过此站点的所有线路
     */
    fun getLinesByStationName(
        stationName: String,
        success: (MetroLines) -> Unit,
        fail: (String) -> Unit
    ) {
        service.getLinesByStationName(stationName)
            .observeOn(Schedulers.io())
            .subscribeOn(AndroidSchedulers.mainThread())
            .doOnError { it.printStackTrace() }
            .subscribe {
                if (it.code == 200) {
                    success(it.data[0])
                } else {
                    fail(it.msg)
                }
            }
    }

    /**
     * 获取经过本站点的所有线路信息,包括此站点的线路的上一站和下一站
     */
    fun getCurrentStationDetail(
        stationName: String,
        success: (List<MetroCurrentStationLines>) -> Unit,
        fail: (String) -> Unit
    ) {
        service.getCurrentStationDetail(stationName)
            .observeOn(Schedulers.io())
            .subscribeOn(AndroidSchedulers.mainThread())
            .doOnError { it.printStackTrace() }
            .subscribe {
                if (it.code==200) {
                    success(it.data)
                }else{
                    fail(it.msg)
                }
            }
    }

    /**
     * 获取此线路的详情信息，包括首发站终点站信息，站点顺序
     */
    fun getStepDetailByLineId(
        lineId: String,
        success: (MetroStep) -> Unit,
        fail: (String) -> Unit
    ) {
        service.getStepDetailByLineId(lineId).observeOn(Schedulers.io())
            .subscribeOn(AndroidSchedulers.mainThread())
            .doOnError { it.printStackTrace() }
            .subscribe {
                if (it.code == 200) {
                    success(it.data)
                } else {
                    fail(it.msg)
                }
            }
    }


}